// pages/ranking/ranking.js
const app = getApp()
const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    switchState:'person',
    uId:"",
    epId:0,
    pageSize:10,
    pageNum:1,
    list:[],
    total:'',
    cur:'person',
    curObject:{},
    showState:false,
    rangeDeps: [{dId: 0, name: '全行'}],
    rangeDepIndex: 0
  },

  changeRangeDep: function (e) {
    let index = e.detail.value
    this.setData({ rangeDepIndex: index})
    this.setData({
      pageNum: 1
    })
    this.organization()
  },

  switchTab: function (e) {
    this.setData({
      pageNum: 1
    })

    this.setData({
      switchState: e.currentTarget.dataset.cur
    })

    this.setData({
      cur: e.currentTarget.dataset.cur  
    })

    this[e.currentTarget.dataset.cur]()
  },
  // 机构榜
  organization() {
    this.setData({
      showState: false
    })

    this.getList(config.service.obtainMatchOrgFocus).then((value) => {
      value.matchDepartmentStatDTOs.forEach((v) => { v.winPer = Math.floor(v.winPer) })
      
      !!value.selfDepartmentStatDTO && (value.selfDepartmentStatDTO['winPer'] = Math.floor(value.selfDepartmentStatDTO['winPer']))

      this.setData({
        list: value.matchDepartmentStatDTOs
      })

      this.setData({
        total: value.total
      })


      this.setData({
        curObject: value.selfDepartmentStatDTO
      })


      if (!value.selfDepartmentStatDTO) return;

      if (!this.data.curObject || this.data.list.findIndex((items) =>  this.data.curObject.dId == items.dId ) > -1) {
        this.setData({
          showState: false
        })
      } else {
        this.setData({
          showState: true
        })
      }


      
    })
  },
  //个人全行
  allbank() {
    this.setData({
      showState: false
    })

    this.getList(config.service.obtainMatchPersonBankFocus).then((value) => {
      this.setData({
        list: value.matchUserStatDTOs
      })

      this.setData({
        curObject: value.matchSelfStatDTO
      })
      if (!this.data.curObject || this.data.list.findIndex((items) => { return this.data.curObject.uId == items.uId }) > -1) {
        this.setData({
          showState: false
        })
      } else {
        this.setData({
          showState: true
        })
      }

      this.setData({
        total: value.total
      })
    })

    
  },
  // 个人部门
  person() {
    this.setData({
      showState: false
    })

    this.getList(config.service.obtainMatchPersonDepartmentFocus).then((value)=>{
      this.setData({
        list: value.matchUserStatDTOs
      })

      this.setData({
        curObject: value.matchSelfStatDTO
      })


      if (!this.data.curObject || this.data.list.findIndex((items) => { return this.data.curObject.uId == items.uId }) > -1) {
        this.setData({
          showState: false
        })
      } else {
        this.setData({
          showState: true
        })
      }

      this.setData({
        total: value.total
      })
    })

    
  },
  

  //获取机构榜范围
  obtainMatchRangeDepartments() {
    let that = this;
    qcloud.request({
      url: config.service.obtainMatchRangeDepartments,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
      },
      login: true,
      success: (response) => {
        let data = response.data
        if (data.code === 0) {
          let rangeDeps = that.data.rangeDeps.concat(data.rangeDeparments)
          that.setData({rangeDeps})
        }
      }
    })
  },

  getList(url) {
    return new Promise((resolve, reject) => {
      qcloud.request({
        url: url,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          'uId': this.data.uId,
          'pageSize': this.data.pageSize,
          'pageNum': this.data.pageNum,
          'epId': this.data.epId,
          'rangeDId': this.data.rangeDeps[this.data.rangeDepIndex].dId
        },
        login: true,
        success: (response) => {
          let data = response.data
          if (data.code === 0) {
            resolve(data)
          } else {
            reject(data.msg)
          }
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (JSON.stringify(options) != '{}') {
      this.setData({
        epId: options.epId
      })
    }

    wx.setNavigationBarTitle({
      title: '排行榜'
    })


    wx.getSystemInfo({
      success:(res)=> {
        this.setData({
          offsetH: res.windowHeight
        })
      }
    })

    this.setData({
      uId: app.globalData.uId
    })

    this.obtainMatchRangeDepartments()

    this.person()
  },

  //下拉滚动加载
  lower() {
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    
    if (this.data.pageNum > Math.ceil(this.data.total / 10)) return false;

    let serverUrl = {
      'person': config.service.obtainMatchPersonDepartmentFocus,
      'allbank': config.service.obtainMatchPersonBankFocus,
      'organization': config.service.obtainMatchOrgFocus
    }

    this.getList(serverUrl[this.data.cur]).then((value) => {
      

      if (this.data.cur == 'organization') {
        value.matchDepartmentStatDTOs.forEach((v) => { v.winPer = Math.floor(v.winPer)})

        !!value.selfDepartmentStatDTO && (value.selfDepartmentStatDTO['winPer'] = Math.floor(value.selfDepartmentStatDTO['winPer']))
        this.setData({
          list: this.data.list.concat(value.matchDepartmentStatDTOs)
        })

        if (!value.selfDepartmentStatDTO) return;
        this.setData({
          curObject: value.selfDepartmentStatDTO
        })

        if (this.data.list.findIndex((items) => { this.data.curObject.dId == items.dId }) > -1) {
          this.setData({
            showState: false
          })
        } else {
          this.setData({
            showState: true
          })
        }
      } else {
        this.setData({
          list: this.data.list.concat(value.matchUserStatDTOs)
        })

        this.setData({
          curObject: value.matchSelfStatDTO
        })

        if (this.data.list.findIndex((items) => { return this.data.curObject.uId == items.uId }) > -1) {
          this.setData({
            showState: false
          })
        } else {
          this.setData({
            showState: true
          })
        }
      }
      this.setData({
        total: value.total
      })
    })
  }
})